<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>权限管理<small>权限管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="权限名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPageByString()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建权限</el-button>
                    </div>

                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="id" label="编号" align="center"></el-table-column>
                        <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
                        <el-table-column prop="keyword" label="权限关键字" align="center"></el-table-column>
                        <el-table-column prop="description" label="权限描述" align="center"></el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>

                    </el-table>
                    <!--分页条-->
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="权限名" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="权限键词" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="权限描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>



                    <!-- 修改标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑权限" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="权限名" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="权限键词" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="权限描述" prop="description" >
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>


        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据  name权限名  keyword权限健词  description权限说明
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '权限名称为必填项', trigger: 'blur' }],
                    keyword: [{ required: true, message: '权限键词为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
            },
            methods: {
                // 重置表单
                resetForm() {
                    //每次点击新建按钮，弹出来的窗口都是新的，不带上次的数据
                    this.formData = {}; //重置数据
                },
                //根据id查询权限
                findById(row){
                    axios.post("/permission/findPermissionById.do?id=" + row.id).then((res)=>{
                        //判断是否查询成功
                        if (res.data.flag){ //查询成功

                            //this.$message.success(res.data.message);
                            //获取查询的数据
                            this.formData= res.data.data;

                        } else {
                            //提示错误信息
                            this.$message.error("查询失败");
                        }

                    })
                },
                handleEdit(row){
                    //点击确定按钮进行表单校验，校验通过然后提交修改
                    this.$refs["dataEditForm"].validate((valid)=>{
                        //判断表单验证是否通过
                        if (valid){ //表单验证通过
                            //发送axios异步请求,添加新的权限到数据库
                            axios.post("/permission/editPermissionById.do",this.formData).then((res)=>{

                                //关闭新建窗口
                                this.dialogFormVisible4Edit = false;
                                //判断是否修改成功
                                if (res.data.flag){ //修改成功
                                    //重新进行分页查询
                                    this.findPage();
                                    //提示成功消息
                                    this.$message.success(res.data.message);
                                }else { //修改失败
                                    //提示失败消息
                                    this.$message.error(res.data.message);
                                }

                            })

                        }else { //表单验证不通过
                            //提示错误信息，表单验证不通过
                            this.$message.error("数据校验失败，请检查您输入的数据是否正确！");
                            //既然这里校验失败了，后面的代码就没有必要执行了，所以直接return false
                            return false;
                        }
                    })

                },
                //修改权限
                handleUpdate(row){
                    this.dialogFormVisible4Edit = true;
                    //弹出窗口后根据id查询当前权限的信息，会显到页面上
                    //发送异步请求，根据id查询权限信息
                    this.findById(row);
                },

                //删除权限
                handleDelete(row) {
                    //使用Element框架的$conﬁrm方法，弹出提示信息
                    this.$confirm('是否删除该权限吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        center: true
                    }).then(() => {     //点击了确定

                        //发送axios异步请求
                        axios.post("/permission/delPermission.do?id=" + row.id).then((res)=>{
                            //判断是否删除成功
                            if (res.data.flag){
                                this.$message.success(res.data.message);
                                //重新分页查询
                                this.findPage();
                            } else{
                                //提示删除失败
                                this.$message.error(res.data.message);
                            }
                        })

                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });

                },
                //新建权限窗口
                handleCreate() {
                    this.resetForm();
                    //点击新建权限，弹出新建权限的窗口
                    this.dialogFormVisible = true;
                },
                //添加权限
                handleAdd() {
                    //点击确定按钮，首先进行表单校验
                    this.$refs["dataAddForm"].validate((valid)=>{
                        //判断表单验证是否通过
                        if (valid){ //表单验证通过
                            //发送axios异步请求,添加新的权限到数据库
                            axios.post("/permission/addPermission.do",this.formData).then((res)=>{

                                //关闭新建窗口
                                this.dialogFormVisible = false;
                                //判断是否添加成功
                                if (res.data.flag){ //添加成功
                                    //重新进行扉分页查询
                                    this.findPage();
                                    //提示成功消息
                                    this.$message.success(res.data.message);
                                }else { //添加失败
                                    //提示失败消息
                                    this.$message.error(res.data.message);
                                }

                            })

                        }else { //表单验证不通过
                            //提示错误信息，表单验证不通过
                            this.$message.error("数据校验失败，请检查您输入的数据是否正确！");
                            //既然这里校验失败了，后面的代码就没有必要执行了，所以直接return false
                            return false;
                        }
                    })

                },
                //分页查询,查询所有的权限
                findPage() {
                    //定义一个param,用来储存分页查询的相关数据
                    var param = {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    }
                    //发送异步请求，将param发送给后台服务器
                    axios.post("/permission/findPage.do", param).then((res) => {
                        if(res.data.flag){  //判断是否查询分页成功
                            //查询成功提示成功的信息
                           /*
                            this.$message({
                                type:"success",
                                message:res.data.message
                            });
                            */

                            //将查询出来的分页数据赋值给 pagination
                            this.pagination.total = res.data.data.total; //将后台返回的中记录数给前台的 total
                            this.dataList = res.data.data.rows; //将后台返回的检查组数据给 dataList 列表数据
                        }else {
                            //查询失败，提示错误信息
                            this.$message.error(res.data.flag);
                        }

                    })
                },
                //根据条件进行分页查询
                findPageByString() {
                    //alert("根据条件查询");
                    //设置当前页码是第一页
                    this.pagination.currentPage = 1;
                    //调用分页查询的方法
                    this.findPage();

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //设置当前页码，然后在调用分页查询的方法
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                }


            }
        })
    </script>
</html>